import requests
import time
from datetime import datetime, timedelta
from lxml import etree
import re
import asyncio
from decimal import Decimal

from .hg_config import HG_Config as config
from data.crawler_data import Game_Info, Game_Odds, flag_dict

class HG:
    def __init__(self):
        self.today_lid = []
        self.early_lid = []
        self.early_detail_request = []
        self.tmpNN = 0
    
    def get_login_uid(self, xml_str = None):
        """登录并从返回的xml中解析出uid"""
        try:
            if xml_str is None:
                # xml_str 是否为空用于判断是否用测试数据
                response = requests.post(config.url, headers=config.headers, cookies=config.cookies, params=config.params, data=config.login_request)
                xml_str = response.text
            root = etree.fromstring(xml_str.encode('utf-8'))
            config.uid = root.find('uid').text          # 获取属性值 uid, 并设置到HG_Config全局变量
            # mid = root.find('mid').text                 # 获取属性值 mid
            # username = root.find('username').text       # 获取属性值 username
            # passwd_safe = root.find('passwd_safe').text # 获取属性值 passwd_safe
            return config.uid
        except Exception as e:
            print(f"登录发生错误: {e}")
            return None

    def query_football_today_lid(self):
        # 获取当前时间戳（毫秒）
        config.football_today_request['ts'] = str(int(time.time() * 1000))
        config.football_today_request['uid'] = config.uid
        config.football_today_request['rtype'] = 'r'
        # 获取今日初盘返回的xml响应
        response = requests.post(config.url, headers=config.headers, cookies=config.cookies, params=config.params, data=config.football_today_request)
        lid = []
        # 解析出需要足球分区数据 xml格式
        root = etree.fromstring(response.text.encode('utf-8'))
        # print(f">>> response: {response.text}")
        ecs = root.xpath('//ec')
        for ec in ecs:
            id = ec.get('id')  # 获取 比赛队伍id 元素
            lid.extend(id[2:].split())
        return lid

    async def get_football_today_detail(self, client, semaphore, lid):
        """异步请求今日初盘的足球数据"""
        async with semaphore:
            try:
                # 获取当前时间戳（毫秒）
                config.football_today_detail_request['ts'] = str(int(time.time() * 1000))
                config.football_today_detail_request['uid'] = config.uid
                config.football_today_detail_request['ecid'] = lid
                # async with client.post(config.url, headers=config.headers, cookies=config.cookies, params=config.params, data=config.football_today_detail_request) as response:
                response = await client.post(config.url, headers=config.headers, cookies=config.cookies, params=config.params, data=config.football_today_detail_request)
                content = None
                content = response.text  # 获取网页内容
                # 解析单场球赛所有数据
                return await self.parseData_detail(content, config.football_today_detail_request['ecid'])
            except Exception as e:
                print(f"请求 {config.url} 时出错：{e}")
                return None


    def query_football_early_lid(self):
        """获取早盘的足球数据中所有lid"""
        # 设置uid, 时间戳(毫秒)
        config.football_early_lid_request['ts'] = str(int(time.time() * 1000))
        config.football_early_lid_request['uid'] = config.uid
        # 获取早盘返回的xml响应 xml格式
        response = requests.post(config.url, headers=config.headers, cookies=config.cookies, params=config.params, data=config.football_early_lid_request)
        # # 解析出所有足球lid
        root = etree.fromstring(response.text.encode('utf-8'))
        classifier = root.find('classifier')
        regions = classifier.findall('region')
        for region in regions:
            leagues = region.findall('league')
            for league in leagues:
                self.early_lid.append(league.get('id'))
        # print(f"lid: {self.early_lid}")

    def query_football_early(self):
        """获取早盘的足球数据"""
        # 获取当前时间戳（毫秒）
        config.football_early_request['ts'] = str(int(time.time() * 1000))
        config.football_early_request['uid'] = config.uid
        
        # 因为每次只能传入8个lid, 所以需要循环请求
        batch_size = 8                                      # 每次处理的条数
        num_batches = len(self.early_lid) // batch_size     # 计算需要循环的次数
        if len(self.early_lid) % batch_size != 0:
            num_batches += 1  # 如果有余数，需要再处理一次
        # 循环处理每一批数据
        for i in range(num_batches):
            # 计算当前批次的起始和结束索引
            start_index = i * batch_size
            end_index = start_index + batch_size
            
            # 截取当前批次的数据
            current_batch = self.early_lid[start_index:end_index]
            config.football_early_request['lid'] = ','.join(current_batch)
            # 获取今日初盘返回的xml响应
            response = requests.post(config.url, headers=config.headers, cookies=config.cookies, params=config.params, data=config.football_early_request)
            # 解析出需要足球分区数据 xml格式
            root = etree.fromstring(response.text.encode('utf-8'))
            ecs = root.findall('ec')
            print(f">>> ecs: {len(ecs)}")
            for ec in ecs:
                game = ec.find('game')
                ecid = game.find('ECID')    # ecid: 请求详情所需要的request参数
                lid = game.find('LID')      # lid: 请求详情所需要的request参数
                self.early_detail_request.append({'ecid': ecid.text, 'lid': lid.text})
        print(f"> 需要抓取的数据: {self.early_detail_request}")
        print(f"> 需要抓取的数据条数: {len(self.early_detail_request)}")

    async def parseData_detail(self, xml_str, lid):
        """解析单场球赛所有数据"""
        odds = []
        gameInfo = Game_Info()
        gameInfo.game_odds = odds
        root = None
        if xml_str is None:
            root = etree.parse('./huangG/所有盘口.xml').getroot()
        else:
            root = etree.fromstring(xml_str.encode('utf-8'))
        games = root.xpath('//game')
        self.tmpNN += 1
        for game in games:
            master = game.attrib['master']
            mode = game.attrib['mode']
            if master == 'Y' and mode == 'FT':
                team_h = await self.parseXML('team_h', game)
                team_c = await self.parseXML('team_c', game)
                print(f'>>> {team_h} vs {team_c} -> {lid} / num: {self.tmpNN}')
                team = team_h + ' vs ' + team_c
                # 开赛时间 皇冠需要+12小时
                original_time_str = await self.parseXML('datetime', game)
                original_time = datetime.strptime(original_time_str, "%Y-%m-%d %H:%M:%S")
                new_time = original_time + timedelta(hours=12)  # 增加12小时
                gameInfo.kick_off_time = new_time.strftime("%Y-%m-%d %H:%M:%S")     # 开赛时间
                gameInfo.league = await self.parseXML('league', game)                     # 联赛
                gameInfo.team_home = team_h                                         # 主队
                gameInfo.team_away = team_c                                         # 客队
                gameInfo.team_id = lid                                              # 比赛id
                gameInfo.team = team                                                # 对战
                gameInfo.source = "皇冠"                                            # 数据来源
                
                # 独赢
                odds.append(Game_Odds('win_h', await self.parseXML('ior_MH', game), flag_dict.get('win_h'), team_h))    # 主队 胜 - 赔率
                odds.append(Game_Odds('win_c', await self.parseXML('ior_MC', game), flag_dict.get('win_c'), team_c))    # 客队 胜 - 赔率
                odds.append(Game_Odds('win_n', await self.parseXML('ior_MN', game), flag_dict.get('win_n')))        # 平局 - 赔率
                
                # 独赢 - 半场
                odds.append(Game_Odds('win_h_half', await self.parseXML('ior_HMH', game), flag_dict.get('win_h_half'), team_h))  # 主队 胜 (半场) - 赔率
                odds.append(Game_Odds('win_c_half', await self.parseXML('ior_HMC', game), flag_dict.get('win_c_half'), team_c))  # 客队 胜 (半场) - 赔率
                odds.append(Game_Odds('win_n_half', await self.parseXML('ior_HMN', game), flag_dict.get('win_n_half')))      # 平局 (半场) - 赔率
                
                # 独赢 & 最先进球
                odds.append(Game_Odds('win_fg_h_h', await self.parseXML('ior_MPGHH', game), flag_dict.get('win_fg_h_h'), f'{team_h},{team_h}'))     # 主队 & 主队 - 赔率
                odds.append(Game_Odds('win_fg_h_c', await self.parseXML('ior_MPGHC', game), flag_dict.get('win_fg_h_c'), f'{team_h},{team_c}'))     # 主队 & 客队 - 赔率
                odds.append(Game_Odds('win_fg_c_h', await self.parseXML('ior_MPGCH', game), flag_dict.get('win_fg_c_h'), f'{team_c},{team_h}'))     # 客队 & 主队 - 赔率
                odds.append(Game_Odds('win_fg_c_c', await self.parseXML('ior_MPGCC', game), flag_dict.get('win_fg_c_c'), f'{team_c},{team_c}'))     # 客队 & 客队 - 赔率
                odds.append(Game_Odds('win_fg_n_h', await self.parseXML('ior_MPGNH', game), flag_dict.get('win_fg_n_h'), team_h))     # 平局 & 主队 - 赔率
                odds.append(Game_Odds('win_fg_n_c', await self.parseXML('ior_MPGNC', game), flag_dict.get('win_fg_n_c'), team_c))     # 平局 & 客队 - 赔率
                
                # # 独赢 & 双方球队进球
                odds.append(Game_Odds('win_btts_h_y', await self.parseXML('ior_MTSHY', game), flag_dict.get('win_btts_h_y'), team_h))   # 主队 & 是 - 赔率
                odds.append(Game_Odds('win_btts_h_n', await self.parseXML('ior_MTSHN', game), flag_dict.get('win_btts_h_n'), team_h))   # 主队 & 否 - 赔率
                odds.append(Game_Odds('win_btts_c_y', await self.parseXML('ior_MTSCY', game), flag_dict.get('win_btts_c_y'), team_c))   # 客队 & 是 - 赔率
                odds.append(Game_Odds('win_btts_c_n', await self.parseXML('ior_MTSCN', game), flag_dict.get('win_btts_c_n'), team_c))   # 客队 & 否 - 赔率
                odds.append(Game_Odds('win_btts_n_y', await self.parseXML('ior_MTSNY', game), flag_dict.get('win_btts_n_y')))   # 平局 & 是 - 赔率
                odds.append(Game_Odds('win_btts_n_n', await self.parseXML('ior_MTSNN', game), flag_dict.get('win_btts_n_n')))   # 平局 & 否 - 赔率
                
                # 大/小 & 双方球队进球
                odds.append(Game_Odds('over_btts_y_1.5', await self.parseXML('ior_OUTAOY', game), flag_dict.get('over_btts_y'), '1.5'))      # 大 & 是 (1.5) - 赔率
                odds.append(Game_Odds('over_btts_n_1.5', await self.parseXML('ior_OUTAOY', game), flag_dict.get('over_btts_n'), '1.5'))      # 大 & 否 (1.5) - 赔率
                odds.append(Game_Odds('under_btts_y_1.5', await self.parseXML('ior_OUTAUY', game), flag_dict.get('under_btts_y'), '1.5'))    # 小 & 是 (1.5) - 赔率
                odds.append(Game_Odds('under_btts_n_1.5', await self.parseXML('ior_OUTAUN', game), flag_dict.get('under_btts_n'), '1.5'))    # 小 & 否 (1.5) - 赔率
                odds.append(Game_Odds('over_btts_y_2.5', await self.parseXML('ior_OUTBOY', game), flag_dict.get('over_btts_y'), '2.5'))      # 大 & 是 (2.5) - 赔率
                odds.append(Game_Odds('over_btts_n_2.5', await self.parseXML('ior_OUTBOY', game), flag_dict.get('over_btts_n'), '2.5'))      # 大 & 否 (2.5) - 赔率
                odds.append(Game_Odds('under_btts_y_2.5', await self.parseXML('ior_OUTBUY', game), flag_dict.get('under_btts_y'), '2.5'))    # 小 & 是 (2.5) - 赔率
                odds.append(Game_Odds('under_btts_n_2.5', await self.parseXML('ior_OUTBUN', game), flag_dict.get('under_btts_n'), '2.5'))    # 小 & 否 (2.5) - 赔率
                odds.append(Game_Odds('over_btts_y_3.5', await self.parseXML('ior_OUTCOY', game), flag_dict.get('over_btts_y'), '3.5'))      # 大 & 是 (3.5) - 赔率
                odds.append(Game_Odds('over_btts_n_3.5', await self.parseXML('ior_OUTCOY', game), flag_dict.get('over_btts_n'), '3.5'))      # 大 & 否 (3.5) - 赔率
                odds.append(Game_Odds('under_btts_y_3.5', await self.parseXML('ior_OUTCUY', game), flag_dict.get('under_btts_y'), '3.5'))    # 小 & 是 (3.5) - 赔率
                odds.append(Game_Odds('under_btts_n_3.5', await self.parseXML('ior_OUTCUN', game), flag_dict.get('under_btts_n'), '3.5'))    # 小 & 否 (3.5) - 赔率
                odds.append(Game_Odds('over_btts_y_4.5', await self.parseXML('ior_OUTDOY', game), flag_dict.get('over_btts_y'), '4.5'))      # 大 & 是 (4.5) - 赔率
                odds.append(Game_Odds('over_btts_n_4.5', await self.parseXML('ior_OUTDOY', game), flag_dict.get('over_btts_n'), '4.5'))      # 大 & 否 (4.5) - 赔率
                odds.append(Game_Odds('under_btts_y_4.5', await self.parseXML('ior_OUTDUY', game), flag_dict.get('under_btts_y'), '4.5'))    # 小 & 是 (4.5) - 赔率
                odds.append(Game_Odds('under_btts_n_4.5', await self.parseXML('ior_OUTDUN', game), flag_dict.get('under_btts_n'), '4.5'))    # 小 & 否 (4.5) - 赔率
                
                # 总进球数
                odds.append(Game_Odds('tg_0-1', await self.parseXML('ior_T01', game), flag_dict.get('tg'), '0-1'))   # 0 - 1 - 赔率
                odds.append(Game_Odds('tg_2-3', await self.parseXML('ior_T23', game), flag_dict.get('tg'), '2-3'))   # 2 - 3 - 赔率
                odds.append(Game_Odds('tg_4-6', await self.parseXML('ior_T46', game), flag_dict.get('tg'), '4-6'))   # 4 - 6 - 赔率
                odds.append(Game_Odds('tg_7', await self.parseXML('ior_OVER', game), flag_dict.get('tg'), '7+'))     # 7 - 以上 - 赔率
                
                # 双胜彩
                odds.append(Game_Odds('dc_h_n', await self.parseXML('ior_DCHN', game), flag_dict.get('dc_h_n'), team_h))  # 主队 | 平局 - 赔率
                odds.append(Game_Odds('dc_c_n', await self.parseXML('ior_DCCN', game), flag_dict.get('dc_c_n'), team_c))  # 客队 | 平局 - 赔率
                odds.append(Game_Odds('dc_h_c', await self.parseXML('ior_DCHC', game), flag_dict.get('dc_h_c'), f'{team_h}, {team_c}'))  # 主队 | 客队 - 赔率
                
                # 落后反超获胜
                odds.append(Game_Odds('cb_h', await self.parseXML('ior_BHH', game), flag_dict.get('cb_h'), team_h))     # 主队 落后反超 - 赔率
                odds.append(Game_Odds('cb_c', await self.parseXML('ior_BHC', game), flag_dict.get('cb_c'), team_c))     # 客队 落后反超 - 赔率
                
                # 双方球队进球
                odds.append(Game_Odds('btts_y', await self.parseXML('ior_TSY', game), flag_dict.get('btts_y')))    # 是 - 赔率
                odds.append(Game_Odds('btts_n', await self.parseXML('ior_TSN', game), flag_dict.get('btts_n')))    # 否 - 赔率
                
                # 双方球队进球 - 半场
                odds.append(Game_Odds('btts_y_half', await self.parseXML('ior_HTSY', game), flag_dict.get('btts_y_half')))    # 是 - 赔率 (半场)
                odds.append(Game_Odds('btts_n_half', await self.parseXML('ior_HTSN', game), flag_dict.get('btts_n_half')))    # 否 - 赔率 (半场)
                
                # 零失球获胜
                odds.append(Game_Odds('cs_win_h', await self.parseXML('ior_WNH', game), flag_dict.get('cs_win_h'), team_h))    # 主队 零失球获胜 - 赔率
                odds.append(Game_Odds('cs_win_c', await self.parseXML('ior_WNC', game), flag_dict.get('cs_win_c'), team_c))    # 客队 零失球获胜 - 赔率
                
                # # 波胆
                score = game.find('PD')
                odds.append(Game_Odds('zero_zero', await self.parseXML('ior_H0C0', score), flag_dict.get('zero_zero')))  # 0:0 - 赔率
                odds.append(Game_Odds('one_one', await self.parseXML('ior_H1C1', score), flag_dict.get('one_one')))    # 1:1 - 赔率
                
                # 罚牌数大小
                card_over_ratio = await self.parseXML('ratio_o', game)      # 大 - 标识
                card_under_ratio = await self.parseXML('ratio_u', game)    # 小 - 标识
                odds.append(Game_Odds('card_over_'+str(card_over_ratio), await self.parseXML('ior_OUH', game), flag_dict.get('card_over'), card_over_ratio))       # 大 - 赔率
                odds.append(Game_Odds('card_under_'+str(card_under_ratio), await self.parseXML('ior_OUC', game), flag_dict.get('card_under'), card_under_ratio)) # 小 - 赔率
                
                # 罚牌数大小 - 半场
                card_over_ratio_half = await self.parseXML('ratio_ho', game)    # 大 - 标识 (半场)
                card_under_ratio_half = await self.parseXML('ratio_hu', game)  # 小 - 标识 (半场)
                odds.append(Game_Odds('card_over_half_'+str(card_over_ratio_half), await self.parseXML('ior_HOUH', game), flag_dict.get('card_over_half'), card_over_ratio_half))       # 大 - 赔率 (半场)
                odds.append(Game_Odds('card_under_half_'+str(card_under_ratio_half), await self.parseXML('ior_HOUC', game), flag_dict.get('card_under_half'), card_under_ratio_half)) # 小 - 赔率 (半场)
                
                # 罚牌数单双
                odds.append(Game_Odds('card_odd', await self.parseXML('ior_HEOO', game), flag_dict.get('card_odd')))      # 单
                odds.append(Game_Odds('card_even', await self.parseXML('ior_HEOE', game), flag_dict.get('card_even')))    # 双
                
                # 让球
                strong = await self.parseXML('strong', game)
                ratio = await self.parseXML('ratio', game)
                strong_home = 'ud_h'
                strong_away = 'ud_c'
                if strong == 'H':
                    strong_home = 'hcap_h'
                elif strong == 'C':
                    strong_away = 'hcap_c'
                odds.append(Game_Odds(str(strong_home)+'_'+str(ratio), await self.parseXML('ior_RH', game), flag_dict.get(strong_home), f'{team_h},{ratio}'))  # 主队让球 - 赔率
                odds.append(Game_Odds(str(strong_away)+'_'+str(ratio), await self.parseXML('ior_RC', game), flag_dict.get(strong_away), f'{team_c},{ratio}'))  # 客队让球 - 赔率
                
                # 让球 - 半场
                hstrong = await self.parseXML('hstrong', game)
                hratio = await self.parseXML('hratio', game)
                hstrong_home = 'ud_h_half'
                hstrong_away = 'ud_c_half'
                if hstrong == 'H':
                    hstrong_home = 'hcap_h_half'
                elif hstrong == 'C':
                    hstrong_away = 'hcap_c_half'
                odds.append(Game_Odds(str(hstrong_home)+'_'+str(hratio), await self.parseXML('ior_HRH', game), flag_dict.get(strong_home), f'{team_h},{hratio}'))  # 主队让球 (半场) - 赔率
                odds.append(Game_Odds(str(hstrong_away)+'_'+str(hratio), await self.parseXML('ior_HRC', game), flag_dict.get(hstrong_away), f'{team_c},{hratio}'))  # 客队让球 (半场) - 赔率
            elif master == 'N' and mode == 'FT':
                # 让球
                strong = await self.parseXML('strong', game)
                ratio = await self.parseXML('ratio', game)
                strong_home = 'ud_h'
                strong_away = 'ud_c'
                if strong == 'H':
                    strong_home = 'hcap_h'
                elif strong == 'C':
                    strong_away = 'hcap_c'
                odds.append(Game_Odds(str(strong_home)+'_'+str(ratio), await self.parseXML('ior_RH', game), flag_dict.get(strong_home), f'{team_h},{ratio}'))  # 主队让球 - 赔率
                odds.append(Game_Odds(str(strong_away)+'_'+str(ratio), await self.parseXML('ior_RC', game), flag_dict.get(strong_away), f'{team_c},{ratio}'))  # 客队让球 - 赔率
                
                # 让球 - 半场
                hstrong = await self.parseXML('hstrong', game)
                hratio = await self.parseXML('hratio', game)
                hstrong_home = 'ud_h_half'
                hstrong_away = 'ud_c_half'
                if hstrong == 'H':
                    hstrong_home = 'hcap_h_half'
                elif hstrong == 'C':
                    hstrong_away = 'hcap_c_half'
                odds.append(Game_Odds(str(hstrong_home)+'_'+str(hratio), await self.parseXML('ior_HRH', game), flag_dict.get(strong_home), f'{team_h},{hratio}'))  # 主队让球 (半场) - 赔率
                odds.append(Game_Odds(str(hstrong_away)+'_'+str(hratio), await self.parseXML('ior_HRC', game), flag_dict.get(hstrong_away), f'{team_c},{hratio}'))  # 客队让球 (半场) - 赔率
        return gameInfo

    async def query_football_today(self, rtype, c_data):
        """获取今日初盘外部的足球数据"""
        # 获取当前时间戳（毫秒）
        config.football_today_request['ts'] = str(int(time.time() * 1000))
        config.football_today_request['uid'] = config.uid
        config.football_today_request['rtype'] = rtype
        # 获取今日初盘返回的xml响应
        response = requests.post(config.url, headers=config.headers, cookies=config.cookies, params=config.params, data=config.football_today_request)
        # 解析出需要足球分区数据 xml格式
        root = etree.fromstring(response.text.encode('utf-8'))

    async def parseData_hot(self, xml_str, c_data):
        """解析热门玩法"""
        root = etree.parse('./huangG/主要玩法.xml').getroot()
        ecs = root.xpath('//ec')
        for ec in ecs:
            game = ec.find('game')  # 获取 比赛
            kick_off_time = await self.parseXML('DATETIME', game) # 开赛时间
            league = await self.parseXML('LEAGUE', game)          # 联赛
            team_h = await self.parseXML('TEAM_H', game)          # 主队
            team_c = await self.parseXML('TEAM_C', game)          # 客队
            team = team_h + ' vs ' + team_c
            c_data.kick_off_time = await self.parseXML('DATETIME', game) # 开赛时间
            c_data.league = await self.parseXML('LEAGUE', game) # 开赛时间
            c_data.team = team # 开赛时间
            
            h_win = await self.parseXML('IOR_MH', game)           # 主赢
            c_win = await self.parseXML('IOR_MC', game)           # 客赢
            n_win = await self.parseXML('IOR_MN', game)           # 和
            hh_win = await self.parseXML('IOR_HMH', game)         # 半场 主赢
            hc_win = await self.parseXML('IOR_HMC', game)         # 半场 客赢
            hn_win = await self.parseXML('IOR_HMN', game)         # 半场 和
            single = await self.parseXML('IOR_EOO', game)         # 单
            double = await self.parseXML('IOR_EOE', game)         # 双
            tsy = await self.parseXML('IOR_TSY', game)            # 双方球队进球 是
            tsn = await self.parseXML('IOR_TSN', game)            # 双方球队进球 否
            print(f"{c_data.kick_off_time} | {c_data.league} | {c_data.team} | {h_win} | {c_win} | {n_win} | {hh_win} | {hc_win} | {hn_win} | {single} | {double} | {tsy} | {tsn}")

    async def parseData_r_big_large(self, xml_str):
        """解析让球&大小"""
        root = etree.parse('./huangG/让球&大小球.xml').getroot()
        ecs = root.xpath('//ec')
        for ec in ecs:
            game = ec.find('game')  # 获取 比赛
            kick_off_time = await self.parseXML('DATETIME', game)         # 开赛时间
            league = await self.parseXML('LEAGUE', game)                  # 联赛
            team_h = await self.parseXML('TEAM_H', game)                  # 主队
            team_c = await self.parseXML('TEAM_C', game)                  # 客队
            team = team_h + ' vs ' + team_c
            
            strong = await self.parseXML('STRONG', game)                  # 让球标识 H:主队让球 C:客队让球
            ratio_flag = await self.parseXML('RATIO_R', game)             # 让球盘口
            odds_h = await self.parseXML('IOR_RH', game)                  # 主队让球赔率
            odds_c = await self.parseXML('IOR_RC', game)                  # 客队让球赔率
            
            a_strong = await self.parseXML('A_sub_STRONG', game)          # 让球标识 H:主队让球 C:客队让球
            a_ratio_flag = await self.parseXML('A_sub_RATIO_R', game)     # 让球盘口
            a_odds_h = await self.parseXML('A_sub_IOR_RH', game)          # 主队让球赔率
            a_odds_c = await self.parseXML('A_sub_IOR_RC', game)          # 客队让球赔率
            
            b_strong = await self.parseXML('B_sub_STRONG', game)          # 让球标识 H:主队让球 C:客队让球
            b_ratio_flag = await self.parseXML('B_sub_RATIO_R', game)     # 让球盘口
            b_odds_h = await self.parseXML('B_sub_IOR_RH', game)          # 主队让球赔率
            b_odds_c = await self.parseXML('B_sub_IOR_RC', game)          # 客队让球赔率
            
            c_strong = await self.parseXML('C_sub_STRONG', game)          # 让球标识 H:主队让球 C:客队让球
            c_ratio_flag = await self.parseXML('C_sub_RATIO_R', game)     # 让球盘口
            c_odds_h = await self.parseXML('C_sub_IOR_RH', game)          # 主队让球赔率
            c_odds_c = await self.parseXML('C_sub_IOR_RC', game)          # 客队让球赔率
            
            big_flag = await self.parseXML('RATIO_OUO', game)             # 大盘口
            large_flag = await self.parseXML('RATIO_OUU', game)           # 小盘口
            big = await self.parseXML('IOR_OUC', game)                    # 大
            large = await self.parseXML('IOR_OUH', game)                  # 小
            
            a_big_flag = await self.parseXML('A_sub_RATIO_OUO', game)     # 大盘口
            a_large_flag = await self.parseXML('A_sub_RATIO_OUU', game)   # 小盘口
            a_big = await self.parseXML('A_sub_IOR_OUC', game)            # 大
            a_large = await self.parseXML('A_sub_IOR_OUH', game)          # 小
            
            b_big_flag = await self.parseXML('B_sub_RATIO_OUO', game)      # 大盘口
            b_large_flag = await self.parseXML('B_sub_RATIO_OUU', game)    # 小盘口
            b_big = await self.parseXML('B_sub_IOR_OUC', game)             # 大
            b_large = await self.parseXML('B_sub_IOR_OUH', game)           # 小
            
            c_big_flag = await self.parseXML('C_sub_RATIO_OUO', game)      # 大盘口
            c_large_flag = await self.parseXML('C_sub_RATIO_OUU', game)    # 小盘口
            c_big = await self.parseXML('C_sub_IOR_OUC', game)             # 大
            c_large = await self.parseXML('C_sub_IOR_OUH', game)           # 小
            
            hstrong = await self.parseXML('HSTRONG', game)                 # 半场 让球标识 H:主队让球 C:客队让球
            hratio_flag = await self.parseXML('RATIO_HR', game)            # 半场 让球盘口
            hratio_h = await self.parseXML('IOR_HRH', game)                # 半场 主队让球赔率
            hratio_c = await self.parseXML('IOR_HRC', game)                # 半场 客队让球赔率
            
            a_hstrong = await self.parseXML('A_sub_HSTRONG', game)         # 半场 让球标识 H:主队让球 C:客队让球
            a_hratio_flag = await self.parseXML('A_sub_RATIO_HR', game)    # 半场 让球盘口
            a_hratio_h = await self.parseXML('A_sub_IOR_HRH', game)        # 半场 主队让球赔率
            a_hratio_c = await self.parseXML('A_sub_IOR_HRC', game)        # 半场 客队让球赔率
            
            b_hstrong = await self.parseXML('B_sub_HSTRONG', game)         # 半场 让球标识 H:主队让球 C:客队让球
            b_hratio_flag = await self.parseXML('B_sub_RATIO_HR', game)    # 半场 让球盘口
            b_hratio_h = await self.parseXML('B_sub_IOR_HRH', game)        # 半场 主队让球赔率
            b_hratio_c = await self.parseXML('B_sub_IOR_HRC', game)        # 半场 客队让球赔率
            
            c_hstrong = await self.parseXML('C_sub_HSTRONG', game)         # 半场 让球标识 H:主队让球 C:客队让球
            c_hratio_flag = await self.parseXML('C_sub_RATIO_HR', game)    # 半场 让球盘口
            c_hratio_h = await self.parseXML('C_sub_IOR_HRH', game)        # 半场 主队让球赔率
            c_hratio_c = await self.parseXML('C_sub_IOR_HRC', game)        # 半场 客队让球赔率
            
            hbig_flag = await self.parseXML('RATIO_HOUO', game)            # 半场 大盘口
            hlarge_flag = await self.parseXML('RATIO_HOUU', game)          # 半场 小盘口
            hbig = await self.parseXML('IOR_HOUC', game)                   # 半场 大
            hlarge = await self.parseXML('IOR_HOUH', game)                 # 半场 小
            
            a_hbig_flag = await self.parseXML('A_sub_RATIO_HOUO', game)    # 半场 大盘口
            a_hlarge_flag = await self.parseXML('A_sub_RATIO_HOUU', game)  # 半场 小盘口
            a_hbig = await self.parseXML('A_sub_IOR_HOUC', game)           # 半场 大
            a_hlarge = await self.parseXML('A_sub_IOR_HOUH', game)         # 半场 小
            
            b_hbig_flag = await self.parseXML('B_sub_RATIO_HOUO', game)    # 半场 大盘口
            b_hlarge_flag = await self.parseXML('B_sub_RATIO_HOUU', game)  # 半场 小盘口
            b_hbig = await self.parseXML('B_sub_IOR_HOUC', game)           # 半场 大
            b_hlarge = await self.parseXML('B_sub_IOR_HOUH', game)         # 半场 小
            
            c_hbig_flag = await self.parseXML('C_sub_RATIO_HOUO', game)    # 半场 大盘口
            c_hlarge_flag = await self.parseXML('C_sub_RATIO_HOUU', game)  # 半场 小盘口
            c_hbig = await self.parseXML('C_sub_IOR_HOUC', game)           # 半场 大
            c_hlarge = await self.parseXML('C_sub_IOR_HOUH', game)         # 半场 小
            print(team)

    async def parseData_corner(self, xml_str):
        """解析角球"""
        root = etree.parse('./huangG/角球.xml').getroot()
        ecs = root.xpath('//ec')
        for ec in ecs:
            game = ec.find('game')  # 获取 比赛
            kick_off_time = await self.parseXML('DATETIME', game)         # 开赛时间
            league = await self.parseXML('LEAGUE', game)                  # 联赛
            team_h = await self.parseXML('TEAM_H', game)                  # 主队
            team_c = await self.parseXML('TEAM_C', game)                  # 客队
            team = team_h + ' vs ' + team_c
            
            h_win = await self.parseXML('IOR_MH', game)                   # 主赢
            c_win = await self.parseXML('IOR_MC', game)                   # 客赢
            n_win = await self.parseXML('IOR_MN', game)                   # 和
            
            hh_win = await self.parseXML('IOR_HMH', game)                 # 半场 主赢
            hc_win = await self.parseXML('IOR_HMC', game)                 # 半场 客赢
            hn_win = await self.parseXML('IOR_HMN', game)                 # 半场 和
            
            single = await self.parseXML('IOR_EOO', game)                 # 单
            double = await self.parseXML('IOR_EOE', game)                 # 双
            
            single = await self.parseXML('IOR_HEOO', game)                # 半场 单
            double = await self.parseXML('IOR_HEOE', game)                # 半场 双
            
            strong = await self.parseXML('STRONG', game)                  # 让球标识 H:主队让球 C:客队让球
            ratio_flag = await self.parseXML('RATIO_R', game)             # 让球盘口
            odds_h = await self.parseXML('IOR_RH', game)                  # 主队让球赔率
            odds_c = await self.parseXML('IOR_RC', game)                  # 客队让球赔率
            
            hstrong = await self.parseXML('HSTRONG', game)                # 半场 让球标识 H:主队让球 C:客队让球
            hratio_flag = await self.parseXML('RATIO_HR', game)           # 半场 让球盘口
            hratio_h = await self.parseXML('IOR_HRH', game)               # 半场 主队让球赔率
            hratio_c = await self.parseXML('IOR_HRC', game)               # 半场 客队让球赔率
            
            big_flag = await self.parseXML('RATIO_OUO', game)             # 大盘口
            large_flag = await self.parseXML('RATIO_OUU', game)           # 小盘口
            big = await self.parseXML('IOR_OUC', game)                    # 大
            large = await self.parseXML('IOR_OUH', game)                  # 小
            
            hbig_flag = await self.parseXML('RATIO_HOUO', game)           # 半场 大盘口
            hlarge_flag = await self.parseXML('RATIO_HOUU', game)         # 半场 小盘口
            hbig = await self.parseXML('IOR_HOUC', game)                  # 半场 大
            hlarge = await self.parseXML('IOR_HOUH', game)                # 半场 小
            print(team)

    async def parseData_card(self, xml_str):
        """解析罚牌数"""
        root = etree.parse('./huangG/罚牌数.xml').getroot()
        ecs = root.xpath('//ec')
        for ec in ecs:
            game = ec.find('game')  # 获取 比赛
            kick_off_time = await self.parseXML('DATETIME', game) # 开赛时间
            league = await self.parseXML('LEAGUE', game)          # 联赛
            team_h = await self.parseXML('TEAM_H', game)          # 主队
            team_c = await self.parseXML('TEAM_C', game)          # 客队
            team = team_h + ' vs ' + team_c
            
            h_win = await self.parseXML('IOR_MH', game)           # 主赢
            c_win = await self.parseXML('IOR_MC', game)           # 客赢
            n_win = await self.parseXML('IOR_MN', game)           # 和
            hh_win = await self.parseXML('IOR_HMH', game)         # 半场 主赢
            hc_win = await self.parseXML('IOR_HMC', game)         # 半场 客赢
            hn_win = await self.parseXML('IOR_HMN', game)         # 半场 和
            single = await self.parseXML('IOR_EOO', game)         # 单
            double = await self.parseXML('IOR_EOE', game)         # 双
            
            big_flag = await self.parseXML('RATIO_OUO', game)             # 大盘口
            large_flag = await self.parseXML('RATIO_OUU', game)           # 小盘口
            big = await self.parseXML('IOR_OUC', game)                    # 大
            large = await self.parseXML('IOR_OUH', game)                  # 小
            
            hbig_flag = await self.parseXML('RATIO_HOUO', game)            # 半场 大盘口
            hlarge_flag = await self.parseXML('RATIO_HOUU', game)          # 半场 小盘口
            hbig = await self.parseXML('IOR_HOUC', game)                   # 半场 大
            hlarge = await self.parseXML('IOR_HOUH', game)                 # 半场 小
            
            strong = await self.parseXML('STRONG', game)                  # 让球标识 H:主队让球 C:客队让球
            ratio_flag = await self.parseXML('RATIO_R', game)             # 让球盘口
            odds_h = await self.parseXML('IOR_RH', game)                  # 主队让球赔率
            odds_c = await self.parseXML('IOR_RC', game)                  # 客队让球赔率
            
            hstrong = await self.parseXML('HSTRONG', game)                 # 半场 让球标识 H:主队让球 C:客队让球
            hratio_flag = await self.parseXML('RATIO_HR', game)            # 半场 让球盘口
            hratio_h = await self.parseXML('IOR_HRH', game)                # 半场 主队让球赔率
            hratio_c = await self.parseXML('IOR_HRC', game)                # 半场 客队让球赔率
            print(team)

    async def parseData_w_big_large(self, xml_str):
        """解析独赢&大小"""
        root = etree.parse('./huangG/独赢&大小.xml').getroot()
        ecs = root.xpath('//ec')
        for ec in ecs:
            game = ec.find('game')  # 获取 比赛
            kick_off_time = await self.parseXML('DATETIME', game)         # 开赛时间
            league = await self.parseXML('LEAGUE', game)                  # 联赛
            team_h = await self.parseXML('TEAM_H', game)                  # 主队
            team_c = await self.parseXML('TEAM_C', game)                  # 客队
            team = team_h + ' vs ' + team_c
            
            big15_flag = "1.5"             # 大盘口
            large15_flag = "1.5"           # 小盘口
            hw15_big = await self.parseXML('IOR_MOUAHO', game)                    # 主队大
            hw15_large = await self.parseXML('IOR_MOUAHU', game)                  # 主队小
            
            cw15_big = await self.parseXML('IOR_MOUACO', game)                    # 客队大
            cw15_large = await self.parseXML('IOR_MOUACU', game)                  # 客队小
            
            nw15_big = await self.parseXML('IOR_MOUANO', game)                    # 平局大
            nw15_large = await self.parseXML('IOR_MOUANU', game)                  # 平局小
            
            big25_flag = "2.5"             # 大盘口
            large25_flag = "2.5"           # 小盘口
            hw25_big = await self.parseXML('IOR_MOUBHO', game)                    # 主队大
            hw25_large = await self.parseXML('IOR_MOUBHU', game)                  # 主队小
            
            cw25_big = await self.parseXML('IOR_MOUBCO', game)                    # 客队大
            cw25_large = await self.parseXML('IOR_MOUBCU', game)                  # 客队小
            
            nw25_big = await self.parseXML('IOR_MOUBNO', game)                    # 平局大
            nw25_large = await self.parseXML('IOR_MOUBNU', game)                  # 平局小
            
            big35_flag = "3.5"             # 大盘口
            large35_flag = "3.5"           # 小盘口
            hw35_big = await self.parseXML('IOR_MOUCHO', game)                    # 主队大
            hw35_large = await self.parseXML('IOR_MOUCHU', game)                  # 主队小
            
            cw35_big = await self.parseXML('IOR_MOUCCO', game)                    # 客队大
            cw35_large = await self.parseXML('IOR_MOUCCU', game)                  # 客队小
            
            nw35_big = await self.parseXML('IOR_MOUCNO', game)                    # 平局大
            nw35_large = await self.parseXML('IOR_MOUCNU', game)                  # 平局小
            print(team)

    async def parseXML(self, node, xml):
        value = xml.find(node)
        if value is not None and value.text is not None:
            if 'ior_HRH' == node or 'ior_HRC' == node or 'ior_RH' == node or 'ior_RC' == node or 'ior_HOUH' == node or 'ior_HOUC' == node or 'ior_OUH' == node or 'ior_OUC' == node:
                return str(Decimal(re.sub(r"[\n\t\r]", "", value.text)) + 1)
            elif 'ratio' == node or 'hratio' == node or 'ratio_o' == node or 'ratio_u' == node or 'ratio_ho' == node or 'ratio_hu' == node:
                return re.sub(r"[\n\t\r]", "", value.text).replace(" ", "")
            else:
                return re.sub(r"[\n\t\r]", "", value.text)
        else:
            return None


if __name__ == "__main__":
    hg = HG()
    xml_str = """<?xml version="1.0" encoding="UTF-8" ?>
        <serverresponse>
        <status>200</status>
        <msg>100</msg>
        <code_message></code_message>
        <username>ckbv577</username>
        <mid>37000065</mid>
        <uid>gn44vipenm37000065l168578b0</uid>
        <passwd_safe>Goblin9527</passwd_safe>
        <ltype>3</ltype>
        <currency>RMB</currency>
        <odd_f>H,M,I,E</odd_f>
        <pay_type>0</pay_type>
        <blackBoxStatus>N</blackBoxStatus>
        <domain>199.26.100.165</domain>
        <t_link>BAD</t_link>
        </serverresponse>
    """
    is_uid = hg.get_login_uid(xml_str)
    print(f"-------------------->>> uid: {config.uid}")
    if is_uid:
        print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
        # hg.query_football_today('r')          # 主要玩法
        # hg.query_football_today('rnou')       # 让球&大小球
        # hg.query_football_today('cn')         # 角球
        # hg.query_football_today('rn')         # 罚牌数
        # hg.query_football_today('pd')         # 波胆
        # hg.query_football_today('moua')       # 波胆
        
        # hg.query_football_today_detail()
        # hg.query_football_early_lid()
        # hg.query_football_early()
        
        # 解析xml数据
        # c_data = Crawler_Data()
        hg.parseData_hot('')
        # hg.parseData_r_big_large('')
        # hg.parseData_corner('')
        # hg.parseData_card('')
        # hg.parseData_w_big_large('')
